﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MbUnit.Framework;
using FlowBasis.EntityFramework;
using System.Data.Metadata.Edm;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

namespace FlowBasisUnitTests.EntityFramework
{
    public class EntityFrameworkMetadataServiceTests
    {
        private SampleDbContext sampleDbContext;

        public EntityFrameworkMetadataServiceTests()
        {
            string sampleDbPath = FlowBasis.Util.AssemblyHelper.GetAssemblyDirectory(typeof(EntityFrameworkMetadataServiceTests).Assembly);
            sampleDbPath += @"\SampleDb.sdf";

            string connectionString = "Data Source=" + sampleDbPath;
            
            Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
            Database.SetInitializer(new DropCreateDatabaseAlways<SampleDbContext>());

            this.sampleDbContext = new SampleDbContext(connectionString);
        }

        [Test]
        public void Test()
        {
            var metadataService = new EntityFrameworkMetadataService(
                () => this.sampleDbContext,
                new Type[] { typeof(SampleDomain.Author), typeof(SampleDomain.Book) });
 
            EntityType entityType = metadataService.TryGetEntityTypeForClrType(typeof(SampleDomain.Author));
            Assert.AreEqual(1, entityType.KeyMembers.Count);
            Assert.AreEqual("AuthorId", entityType.KeyMembers[0].Name);
        }
    }
}
